{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Functions [demonstration]\n",
    "You just saw code that looks like the code below. Hopefully you found that by changing the value of num_trials you could also change how close the overall \"heads percentage\" was to 50%. By running more and more trials, you would get closer and closer to a consistent 50%."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random as rd\n",
    "\n",
    "num_trials = 100\n",
    "heads = 0\n",
    "tails = 0\n",
    "p_heads = 0.5\n",
    "\n",
    "for i in range(num_trials):\n",
    "    random_number = rd.random()\n",
    "    if random_number < p_heads:\n",
    "        heads = heads + 1\n",
    "    else:\n",
    "        tails += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, we're going to **encapsulate** this code by putting it into a function we define. The code below shows how a function is **defined** and **called** in Python.\n",
    "\n",
    "As you read through the code below, pay attention to the following:\n",
    "\n",
    "1. **Whitespace / indentation**. Many programming languages use braces {}, parentheses (), brackets <>, etc... to delimit a block of code. Python does not! \n",
    "2. **Defining vs. Calling a function**. All the code between lines 3-14 is the *definition* of `simulate_coin_flips` and line 16 shows how you *call* `simulate_coin_flips`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.485\n"
     ]
    }
   ],
   "source": [
    "import random as rd\n",
    "\n",
    "def simulate_coin_flips(num_trials):\n",
    "    heads = 0\n",
    "    tails = 0\n",
    "    p_heads = 0.5\n",
    "    for i in range(num_trials):\n",
    "        random_number = rd.random()\n",
    "        if random_number < p_heads:\n",
    "            heads = heads + 1\n",
    "        else:\n",
    "            tails += 1\n",
    "    percent_heads = heads / num_trials\n",
    "    return percent_heads\n",
    "    \n",
    "percentage = simulate_coin_flips(200) # calling the function\n",
    "print(percentage)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You now have a new tool you can use! If you want to try calling this function with different values for num_trials, you can."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.46\n"
     ]
    }
   ],
   "source": [
    "print(simulate_coin_flips(100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.4964\n"
     ]
    }
   ],
   "source": [
    "print(simulate_coin_flips(10000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.499917\n"
     ]
    }
   ],
   "source": [
    "print(simulate_coin_flips(1000000))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
